package com.tledu.production.controller.plan;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tledu.production.core.result.AjaxResult;
import com.tledu.production.model.plan.JobManagement;
import com.tledu.production.service.plan.PlanService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@AllArgsConstructor
@RequestMapping("/JobController")
public class JobController {
    private final PlanService planService;
    @GetMapping("/plan/jobManagement")
    public String getAllJ(Model model,Integer pageNum, Integer pageSize ){
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 4;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<JobManagement> job=planService.getAllJ();
        PageInfo<JobManagement> page = PageInfo.of(job);//把获取到的数据封装到page中
        model.addAttribute("getAllJ",page.getList());
        model.addAttribute("page", page);
        model.addAttribute("pages",new int[page.getPages()]);
        return "plan/JobManagement";
    }
    @GetMapping ("/plan/job-add")
    public String add(){
        return"plan/add/job-add";
    }
    @ResponseBody
    @PostMapping("/plan/job-add")
    public AjaxResult<Void> addJ(JobManagement jobManagement){
        planService.insertJ(jobManagement);
        return new AjaxResult<>();
    }
//  **********************编辑**************************
    @GetMapping("edit/job-select/selectJ/{workId}")
    public String selectJ(Model model,@PathVariable Integer workId){
        JobManagement jobManagement=planService.selectJ(workId);
        model.addAttribute("jobManagement",jobManagement);
        return "plan/edit/job-edit";
    }
    @PostMapping("/edit/job-edit/updateJ")
    @ResponseBody
    public AjaxResult<Void> updateJ(JobManagement jobManagement){
        planService.updateJ(jobManagement);
        return new AjaxResult<>();
    }
}
